<?php

namespace App\Http\Controllers\User;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Plugin\Models\Java\PdLevel;
use App\Http\Controllers\Plugin\Models\User\UnbindUserConfig;
use App\Models\ChangeJuniorLog;
use App\Models\ConfigTable;
use App\Models\Java\PdUserRelationship;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class UserInvoidLoginController extends Controller
{
    //
    use ResponseData;
    public function loginTimeout(){
        $un_login_data = UnbindUserConfig::query()
            ->select('level','no_login_days')
            ->where('no_login_days','>',0)
            ->get();
        info('获取到的未登录用户'.json_encode($un_login_data));
        if($un_login_data){
            foreach ($un_login_data as $item){
                $this->setDays($item->no_login_days,$item->level);
            }
        }
        // 30天未下单用户
        $on_order_data = UnbindUserConfig::query()
            ->select('level','no_order_place')
            ->where('no_order_place','>',0)
            ->get();
        info('获取到未下单用户'.json_encode($on_order_data));
        if($on_order_data){
            foreach ($on_order_data as $item){
                $this->noOrder($item->no_order_place,$item->level);
            }
        }

        return $this->success('获取数据');
    }
    protected function noOrder($days,$levels){
        $thirtyDaysAgo = now()->subDays($days);
        $getData = User::query()
            ->whereDoesntHave('orders', function ($query) use ($thirtyDaysAgo) {
                $query->where('created_at', '>=', $thirtyDaysAgo);
            })
            ->select('id','pid','pid_collect','openid')
            ->where('pid','>',0) // 有上级
            ->whereNotNull('openid')
            ->where('openid','<>','')
            ->where('is_robot',1)// 不是机器人
            ->get();
        info('获取无订单数据'.json_encode($getData));
//        return $this->success($getData);
        if($getData->count()>0){
            foreach ($getData as $item){
                $isLevel = $this->checkPidLevel($item->pid,$levels);
                if($isLevel){
                    // 如果命中则创建机器人
                    $robot = $this->createRebot($item->id,$item->pid,$item->pid_collect,$isLevel);
                    // 日志记录
                    if($robot){
                        $this->setLog($item->id,$robot->id,$days.'天未下单用户');
                        $item->pid_collect = 0;
                        $item->pid = 0;
                        $item->x_bind = 2;
                        $item->save();
                    }
                }
            }
        }
    }
    protected function setDays($days,$levels){
        $getData = User::query()
            ->select('id','pid','pid_collect','openid')
            ->where('updated_at','<',now()->subDays($days)) // 超过配置文件天数未登录
            ->where('pid','>',0) // 有上级
            ->where('is_robot',1)// 不是机器人
            ->get();
        info('30天未登录用户'.json_encode($getData));
        if($getData->count()>0){
            foreach ($getData as $item){
                info('获取当前用户'.json_encode($item));
                $isLevel = $this->checkPidLevel($item->pid,$levels);
                info('当前用户父级'.json_encode($isLevel));
                if($isLevel){
                    // 如果命中则创建机器人
                    $robot = $this->createRebot($item->id,$item->pid,$item->pid_collect,$isLevel);
                    // 日志记录
                    if($robot){
                        info('获取添加机器人数据'.json_encode($robot));
                        $this->setLog($item->id,$robot->id,$days.'天未登录用户');
                        $item->pid_collect = 0;
                        $item->pid = 0;
                        $item->x_bind = 2;
                        $item->save();
                    }
                }
            }
        }
    }
    protected function setLog($uid,$romot_id,$reason){
        $data = [
            'user_id'=>$uid,
            'robot_id'=>$romot_id,
            'reason'=>$reason,
        ];
        ChangeJuniorLog::query()->create($data);
    }
    // 查询当前用户父级是否存在配置文件中
    protected function checkPidLevel($nid,$levels){
        $level = PdLevel::query()
            ->whereIn('level',$levels)
            ->where('mode',-1)
            ->pluck('threshold');
        return PdUserRelationship::query()->where('nid',$nid)->whereIn('level',$level)->first();
    }
    // 建立机器人
    protected function createRebot($uid,$pid,$pid_collect,$isLevel){
        DB::beginTransaction();
        $acc_user_level = PdUserRelationship::query()->where('nid',$uid)->first();
        $name = 'rebot'.mt_rand(1,2000);
        $rebot = [
            'avatar'=>'https://zhixuekejimap.oss-cn-beijing.aliyuncs.com/avatar/6D9ZAE1HZI.jpg',
            'name'=>$name,
            'is_robot'=>2,
            'pid'=>$pid,
            'pid_collect'=>$pid_collect
        ];
        $user = User::query()->create($rebot);
        if($user){
            $r_uid = 'romot_'.Str::random(16);
            // 绑定用户上级
            $remoteUser = [
                'name' =>$name,
                'gid' => $isLevel->gid,
                'uid' => $r_uid,
                'pid' => $isLevel->uid,
                'nid' =>$user->id,
                'level'=>$acc_user_level->level
            ];
            // 创建机器人
            PdUserRelationship::query()->create($remoteUser);
            // 找到原用户
            $u_openid = PdUserRelationship::query()->where('nid',$uid)->value('uid');
            // 更新用户上级
            PdUserRelationship::query()->where('pid',$u_openid)->update(['pid'=>$r_uid]);
            $acc_user_level->pid = $isLevel->gid;
            $acc_user_level->save();
        }
        DB::commit();
        return $user;
    }
}
